package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.utils.BaseContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    //添加购物车
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //根据用户ID 和菜品ID || 套餐ID 去查询是否有这个菜品 || 套餐
        //1.构建条件对象
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        //1.2设置条件,用户ID
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //判断传入的是否是菜品ID值还是套餐ID
        if (shoppingCart.getDishId() != null) {
            //1.3设置菜品ID
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //1.4设置套餐ID
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //2.查询购物车列表
        //getOne只有一条查询信息返回到情况下
        ShoppingCart cartInDB = getOne(lqw);

        //3.判断是添加还是更新
        if (cartInDB == null) {
            //表里(购物车)没有记录,添加更新操作
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            save(shoppingCart);
            cartInDB = shoppingCart;
        } else {
            //如果表里已经被用户加入过了购物车数据
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            updateById(cartInDB);
        }

        return cartInDB;

    }

    //删除购物车
    @Override
    public ShoppingCart update(ShoppingCart shoppingCart) {
        //1.构建条件对象
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        //2.判断传入的是菜品id还是套餐id

        //若是菜品ID
        if (shoppingCart.getDishId() != null) {
            //根据菜品ID拿到菜品数据
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart dish = getOne(lqw);

            //判断菜品添加数量是否有一个以上,可修改数量至0
            if(dish.getNumber()>0){
                dish.setNumber(dish.getNumber()-1);
            }

            //如果购物车的菜品数量为零时就删除记录
            if(dish.getNumber()==0){
                removeById(dish.getId());
            }

            //修改数据并返回
            updateById(dish);
            return dish;
        }

        //若是套餐ID
        if (shoppingCart.getSetmealId() != null) {
            //根据菜品ID查菜品数据
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart setmeal = getOne(lqw);

            //判断套餐添加数量是否有一个以上,可修改数量至0
            if(setmeal.getNumber()>0){
                setmeal.setNumber(setmeal.getNumber()-1);
            }

            //如果购物车的套餐数量为零时就删除记录
            if (setmeal.getNumber()==0){
                removeById(setmeal.getId());
            }

            //修改数据并返回
            updateById(setmeal);
            return setmeal;
        }
        return null;
    }

}